package com.tmall.testgen.plugin.wizards.runinspring;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.tmall.testgen.client.ClientDataCenter;
import com.tmall.testgen.client.runinspring.RunInSpringHandler;
import com.tmall.testgen.plugin.wizards.GenTestCodeWizadPage;
import com.tmall.testgen.plugin.wizards.MyMessageDialog;

/**
 * 自定义相对对话框
 * 
 * @author liuzx
 * 
 */
public class CreateTestWizardDialog extends WizardDialog {

	private RunInSpingWizad runInSpingWizad;

	public CreateTestWizardDialog(Shell parentShell, IWizard newWizard) {
		super(parentShell, newWizard);
		// setShellStyle(SWT.MODELESS | SWT.MIN | getShellStyle());
		setShellStyle(SWT.MIN | SWT.CLOSE | SWT.MAX | SWT.TITLE | SWT.BORDER
				| SWT.MODELESS | SWT.RESIZE | getDefaultOrientation());
		if (newWizard instanceof RunInSpingWizad) {
			runInSpingWizad = (RunInSpingWizad) newWizard;
		}

	}

	protected void createButtonsForButtonBar(Composite parent) {
		super.createButtonsForButtonBar(parent);
		getButton(IDialogConstants.NEXT_ID).setText("生成测试");
		getButton(IDialogConstants.NEXT_ID).setEnabled(false);
	}

	protected void buttonPressed(final int buttonId) {
		if (buttonId == IDialogConstants.NEXT_ID) {
			IWizardPage page = runInSpingWizad.getPage("GenTestCodeWizadPage");
			if (page != null && (page instanceof GenTestCodeWizadPage)) {
				GenTestCodeWizadPage wp = (GenTestCodeWizadPage) page;
				if (ClientDataCenter.getInstance().getInfoes() == null
						|| ClientDataCenter.getInstance().getInfoes().size() == 0) {
					MyMessageDialog.openWarnDialog(wp.getShell(),
							"干啥玩意啊，测试信息没收集呢就要生成单元测试啊，泡妹子也没有这么着急的！");
					return;
				}
				wp.initDataInfoList(ClientDataCenter.getInstance().getDbInfos());
			}
		}
		super.buttonPressed(buttonId);
		// final CreateTestWizardDialog dialog = this;
		// if (buttonId == IDialogConstants.NEXT_ID) {
		// IRunnableWithProgress op = new IRunnableWithProgress() {
		// public void run(final IProgressMonitor monitor)
		// throws InvocationTargetException {
		// try {
		// monitor.beginTask("gen test code ", 2);
		// Display.getDefault().syncExec(new Runnable() {
		//
		// @Override
		// public void run() {
		// IWizardPage page = runInSpingWizad
		// .getPage("GenTestCodeWizadPage");
		// if (page != null
		// && (page instanceof GenTestCodeWizadPage)) {
		// GenTestCodeWizadPage wp = (GenTestCodeWizadPage) page;
		// if (ClientDataCenter.getInstance().getInfoes() == null
		// || ClientDataCenter.getInstance()
		// .getInfoes().size() == 0) {
		// MyMessageDialog.openWarnDialog(wp.getShell(),
		// "干啥玩意啊，测试信息没收集呢就要生成单元测试啊，泡妹子也没有这么着急的！");
		// return;
		// }
		// monitor.worked(1);
		// wp.initDataInfoList(ClientDataCenter.getInstance()
		// .getDbInfos());
		// monitor.worked(1);
		// dialog.nextPressed();
		// }
		// }
		// });
		// } catch (Exception e) {
		// throw new InvocationTargetException(e);
		// } finally {
		// monitor.done();
		// }
		// }
		// };
		// try {
		// run(true, false, op);
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		// }else{
		// super.buttonPressed(buttonId);
		// }

	}

}
